package com.sniper.springmvc.shiro.session.redis;

import com.sniper.springmvc.redis.JedisConnectionFactory;
import com.sniper.springmvc.utils.DataUtil;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Redis缓存
 *
 * @param <K>
 * @param <V>
 * @author suzhen
 */
public class RedisCache<K, V> implements MyCache<K, V> {

    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);
    /**
     * The wrapped Jedis instance.
     */
    private JedisConnectionFactory cache;
    private long ttlTime = 1800000l;


    /**
     * The Redis key prefix for the sessions
     */
    private String keyPrefix = "shiro_redis_session:";

    /**
     * Returns the Redis session keys prefix.
     *
     * @return The prefix
     */
    public String getKeyPrefix() {
        return keyPrefix;
    }

    /**
     * Sets the Redis sessions key prefix.
     *
     * @param keyPrefix The prefix
     */
    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }

    /**
     * 通过一个JedisManager实例构造RedisCache
     */
    public RedisCache() {

    }

    public RedisCache(JedisConnectionFactory cache) {
        this.cache = cache;
    }

    public RedisCache(JedisConnectionFactory cache, String keyPrefix) {
        this.cache = cache;
        this.keyPrefix = keyPrefix;
    }

    public RedisCache(JedisConnectionFactory cache, long ttlTime, String keyPrefix) {
        this.cache = cache;
        this.ttlTime = ttlTime;
        this.keyPrefix = keyPrefix;
    }


    /**
     * 获得byte[]型的key
     *
     * @param key
     * @return
     */
    private byte[] getByteKey(K key) {
        if (key instanceof String) {
            String preKey = this.keyPrefix + key;
            return preKey.getBytes();
        } else {
            return DataUtil.byteMerger(keyPrefix.getBytes(), SerializeUtils.serialize(key));
        }
    }

    @Override
    public V get(K key) throws CacheException {
        logger.debug("根据key从Redis中获取对象 key [" + key + "]");
        try {
            if (key == null) {
                return null;
            } else {
                byte[] rawValue = cache.get(getByteKey(key));
                V value = (V) SerializeUtils.deserialize(rawValue);
                return value;
            }
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    /**
     * 一般应该设置一个默认的储存时间
     *
     * @param key
     * @param value
     * @return
     * @throws CacheException
     */
    @Override
    public V put(K key, V value) throws CacheException {
        logger.debug("根据key从存储 key [" + key + "]");
        try {
            cache.set(getByteKey(key), SerializeUtils.serialize(value), ttlTime);
            return value;
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public V remove(K key) throws CacheException {
        logger.debug("从redis中删除 key [" + key + "]");
        try {
            V previous = get(key);
            cache.del(getByteKey(key));
            return previous;
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public void clear() throws CacheException {

        logger.debug("从redis中删除所有元素");
        try {
            cache.flushDB();
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public int size() {
        try {
            Long longSize = new Long(cache.dbSize());
            return longSize.intValue();
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public Set<K> keys() {
        try {
            Set<byte[]> keys = cache.keys((this.keyPrefix + "*").getBytes());
            if (CollectionUtils.isEmpty(keys)) {
                return Collections.emptySet();
            } else {
                Set<K> newKeys = new HashSet<K>();
                for (byte[] key : keys) {
                    newKeys.add((K) key);
                }
                return newKeys;
            }
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    @Override
    public Collection<V> values() {
        try {
            Set<byte[]> keys = cache.keys((this.keyPrefix + "*").getBytes());
            if (!CollectionUtils.isEmpty(keys)) {
                List<V> values = new ArrayList<V>(keys.size());
                for (byte[] key : keys) {
                    V value = get((K) key);
                    if (value != null) {
                        values.add(value);
                    }
                }
                return Collections.unmodifiableList(values);
            } else {
                return Collections.emptyList();
            }
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }


    @Override
    public void put(K k, V v, long time) throws CacheException {
        logger.debug("根据key从存储 key [" + k + "]");
        try {
            cache.set(getByteKey(k), SerializeUtils.serialize(v), time);
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }


}
